<template>
    <div>
        <Header :left="true" leftText="返回" title="质量称重" > </Header>
        <van-form >
                <van-field
                readonly
                required
                class="formstyle"
                :value="form.equipKey"
                name="设备码"
                label="设备码"
                placeholder="设备码"
            />
            <van-field
                readonly
                required
                class="formstyle"
                :value="form.createTime"
                name="录入时间"
                label="录入时间"
                placeholder="录入时间"
            />
            <van-field
            required
                class="formstyle"
                v-model="form.productName"
                name="产品名称"
                label="产品名称"
                placeholder="产品名称"
            />
            <van-row gutter="2">
                <van-col span="12">
                    <van-field
                        required
                        class="formstyle"
                        v-model="standardSingleweightDOWN"
                        name="标准单重下线(g)"
                        label="标准单重下线(g)"
                        placeholder="标准单重下线(g)"
                />
                </van-col>

                <van-col span="12">
                    <van-field
                        required
                        class="formstyle"
                        v-model="standardSingleweightUP"
                        name="标准单重上线(g)"
                        label="标准单重上线(g)"
                        placeholder="标准单重上线(g)"
                />
                </van-col>
                
            </van-row>
            
        <NoticeBar color="#1989fa" background="#ecf9ff" style="margin-top: 5px; font-size: 15px; font-weight: bold;margin-bottom: 5px;"
        left-icon="description" >单重(g)</NoticeBar>
        <van-row style="margin-bottom: 5px;" v-for="(item,index) in weighingList" :key="index" gutter="2">
            <van-col span="4" style="text-align:center;">
                 第{{ index+1 }}项
            </van-col>
            <van-col span="14">
                 <van-field  v-model="weighingList[index]"  />
            </van-col>
           <van-col span="5">
            <div style="width: 80%;margin: 0 auto;">
                <van-button :block="true"  round type="danger" @click="deleteOne(index)" >删除</van-button>
            </div>
           </van-col>
           
        </van-row>
        <van-row style="margin-top: 5px;">
            <div style="width: 80%;margin: 0 auto;">
                 <van-button type="warning" @click="addForm" style="width: 100%;">添加</van-button>
            </div>
        </van-row>

        <van-row style="margin-top: 20px;">
            <div style="width: 80%;margin: 0 auto;">
                 <van-button type="primary"  style="width: 100%;" @click="submit">提交</van-button>
            </div>
        </van-row>
        <br/>
        <br/>
        <br/>
        <br/>
     </van-form>
    </div>
</template>

<script>

import {
  Field,
  Form,
  Row,
  Col,
  Toast,
  NoticeBar,
  Button
} from "vant";

import "vue-easytable/libs/themes-base/index.css";

import { _httpClient  } from "@/libs/api";
import Header from "@/components/header.vue";

Vue.use(Form);
Vue.use(Field);
Vue.use(NoticeBar);
Vue.use(Button);
Vue.use(Row);
Vue.use(Col);
export default {
    name: 'qWeighing',
    components: {
    Field,
    NoticeBar,
    Form,
    Toast,
    Header,
    Button,
    Row,
    Col
  },
    data() {
        return {
            form:{
                equipKey:"",
                createTime:null,
                productName:"",
                standardSingleweight:""
            },
            standardSingleweightUP:0,
            standardSingleweightDOWN:0,
            weighingList:[]
        };
    },

    created() {
        //打开扫描
        this.showScanner()
    },

    methods: {
        showScanner() {
     
            //alert(api.deviceName.toLowerCase());
            var _this = this;
            if (
            api.deviceName.toLowerCase() == "huawei" ||
            api.deviceName.toLowerCase() == "honor"
            ) {
            var zyHmsScan = api.require('zyHmsScan');
            var param = {
                //returnAll:true
            };
            zyHmsScan.openDefaultView(param, function (ret) {
                _this.toDetail(ret.result);
                
            });
            }
            else {
            var FNScanner = _this.$ac.require("FNScanner");
            FNScanner.openScanner(
                {
                autorotation: true,
                },
                function (ret, err) {
                if (ret) {
                    if (ret.eventType == "success" && ret.content != "") {
                    _this.toDetail(ret.content);
                    } else if (ret.eventType == "cancel") {
                    _this.$ac.closeWin();
                    }
                } else {
                    _this.$ac.closeWin();
                }
                }
            );
            }
        },
        addForm(){
            if(this.weighingList.length==20){
                Toast.clear()
                Toast.fail("称重最多20个")
                return
            }
            this.weighingList.push("")
        },
        async toDetail(key){
            // alert(key)
            this.form.equipKey=key

            let res=await _httpClient.get(
                _httpClient.getApiPrefix()+"/system/weighing/getOne/"+key 
            )
            // alert(JSON.stringify(res))

            if(res.code === 200){
                if(res.data!=null){
                    this.form.productName=res.data.productName
                }
            }else{
                Toast.clear()
                Toast.fail(res.msg)
            }

            this.form.createTime=this.formatDate(new Date())
        },
    formatDate(date) {
      let year = date.getFullYear()
      let mouth = date.getMonth() + 1
      let day = date.getDate()
      let hour = date.getHours()
      let min = date.getMinutes()
      if (mouth < 10) {
        mouth = '0' + mouth
      }
      if (day < 10) {
        day = '0' + day
      }
      if (hour < 10) {
        hour = '0' + hour
      }
      if (min < 10) {
        min = '0' + min
      }
      return year + '-' + mouth + '-' + day + ' ' + hour + ':' + min + ':' + '00'
    },
    deleteOne(index){
       this.weighingList.splice(index,1)
    },
    async submit(){


        if(this.form.productName==""){
            Toast.clear()
            Toast.fail("产品名称不能为空")
            return
        }
        if(this.standardSingleweightUP==""){
            Toast.clear()
            Toast.fail("标准单重上线不能为空")
            return
        }

        if(this.standardSingleweightDOWN==""){
            Toast.clear()
            Toast.fail("标准单重下线不能为空")
            return
        }

        if(Object.is(Number(this.standardSingleweightUP), NaN)){
            Toast.clear()
            Toast.fail("标准单重上线必须是数字") 
            return
        }

        if(Object.is(Number(this.standardSingleweightDOWN), NaN)){
            Toast.clear()
            Toast.fail("标准单重下线必须是数字") 
            return
        }

        this.form.standardSingleweight=this.standardSingleweightDOWN+"-"+this.standardSingleweightUP

        if(this.weighingList.length==0){
            Toast.clear()
            Toast.fail("质量不能为空") 
            return
        }

        for(let i=0;i<this.weighingList.length;i++){
            let item=this.weighingList[i]
            if(Object.is(Number(item), NaN)){
            Toast.clear()
            Toast.fail(`重量第${i+1}项不是数字`) 
            return
            }else{
                this.weighingList[i]=Number(item)
            }
        }
        this.form.weighingData=this.weighingList.join(";")

        //提交
        try{
            
            let res = await _httpClient.post(
            _httpClient.getApiPrefix() + "/system/weighing",
            {
                body: this.form
            });

            if(res.code === 200){
                Toast.clear()
                Toast.fail(`新增成功`)
                setTimeout(()=>{
                        api.closeWin();
                },500)
            
            }else{
                Toast.clear()
                Toast.fail(res.msg)
            }
        }catch(e){
            Toast(JSON.stringify(e))
        }
    }
    }
};
</script>

<style  scoped>
body {
    background-color: white;
}


</style>